package com.app.business.web.business;

import com.app.business.en.HouseStatus;
import com.app.business.model.*;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.dataType.BigDecimalUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.IntegerUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ddz on 2019/3/13.
 */
@Controller
@RequestMapping("/business/houseView/*")
@SuppressWarnings("all")
public class HouseViewController {
    @Resource
    private HouseService houseService;
    @Resource
    private DataDictService dataDictService;
    @Resource
    private AzProjectService azProjectService;
    @Resource
    private StorageRoomService storageRoomService;
    @Resource
    private CarLotService carLotService;
    @Resource
    private BuyHousePersonService buyHousePersonService;
    @Resource
    private HouseBuyPersonService houseBuyPersonService;
    @Resource
    private CarLotBuyPersonService carLotBuyPersonService;
    @Resource
    private StatisticsService statisticsService;

    /**
     * 安置房源项目查看列表
     */
    @RequestMapping("azProjectList")
    @RequiresPermissions("houseView:azProjectList")
    public void azProjectList() {
    }


    /**
     * 加载项目信息数据
     *
     * @return
     */
    @RequestMapping("loadAzProjectList")
    @ResponseBody
    @RequiresPermissions("houseView:loadAzProjectList")
    public Map loadAzProjectList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<AzProject> azProjectPageBean = this.azProjectService.getAzProjectPageBean();
        Map<String, String> townMap = dataDictService.getMapByParentNameForValue_Name("乡镇");
        List<AzProject> azProjectList = azProjectPageBean.getDatas();
        if (!CollectionUtils.isEmpty(azProjectList)) {
            Map<String, Map> houseCountMap = this.houseService.getHouseCountMap();
            Map<String, Map> storageRoomCountMap = this.storageRoomService.getStorageRoomCountMap();
            Map<String, Map> carLotCountMap = this.carLotService.getCarLotCountMap();
            Map<String, Object> item = null;
            for (AzProject azProject : azProjectList) {
                item = null;
                item = Maps.newHashMap();
                Map houseMap = CollectionUtils.isEmpty(houseCountMap.get(azProject.getId().toString())) ? Maps.newHashMap() : houseCountMap.get(azProject.getId().toString());
                Map storageRoomMap = CollectionUtils.isEmpty(storageRoomCountMap.get(azProject.getId().toString())) ? Maps.newHashMap() : storageRoomCountMap.get(azProject.getId().toString());
                Map carLotMap = CollectionUtils.isEmpty(carLotCountMap.get(azProject.getId().toString())) ? Maps.newHashMap() : carLotCountMap.get(azProject.getId().toString());
                String houseTotal = houseMap.get("houseTotal") == null ? "0" : houseMap.get("houseTotal").toString();
                String signHouse = houseMap.get("signHouse") == null ? "0" : houseMap.get("signHouse").toString();
                String storageRoomTotal = storageRoomMap.get("storageRoomTotal") == null ? "0" : storageRoomMap.get("storageRoomTotal").toString();
                String signStorageRoom = storageRoomMap.get("signStorageRoom") == null ? "0" : storageRoomMap.get("signStorageRoom").toString();
                String carLotTotal = carLotMap.get("carLotTotal") == null ? "0" : carLotMap.get("carLotTotal").toString();
                String signCarLot = carLotMap.get("signCarLot") == null ? "0" : carLotMap.get("signCarLot").toString();
                item.put("id", azProject.getId());
                item.put("projectCode", azProject.getProjectCode());
                item.put("projectName", azProject.getProjectName());
                item.put("town", StringUtils.getNotNull(townMap.get(azProject.getTown())));
                item.put("houseCount", houseTotal + "【<span class='jmdf-color-red'>" + signHouse + "</span>】");
                item.put("storageRoomCount", storageRoomTotal + "【<span class='jmdf-color-red'>" + signStorageRoom + "</span>】");
                item.put("carLotCount", carLotTotal + "【<span class='jmdf-color-red'>" + signCarLot + "</span>】");

                listData.add(item);
            }
        }
        return LayUiUtils.page(azProjectPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 楼盘图界面
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("housePicture")
    @RequiresPermissions("houseView:housePicture")
    public void housePicture(HttpServletRequest request, ModelMap modelMap) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        Precondition.checkSqdsArguement(azProject != null, "数据错误！");
        List<String> buildNumList = this.houseService.getBuildNumListByAzProjectId(azProject.getId());
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("buildNumList", buildNumList);
    }


    //房源信息
    @RequestMapping("loadHousePicture")
    @ResponseBody
    @RequiresPermissions("houseView:loadHousePicture")
    public Map loadHousePicture(HttpServletRequest request) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        String buildNum = ParamUtils.getString(request, "buildNum", "");

        //通过征收项目、安置项目、楼号得到所分配的houseList
        List<House> houseList = houseService.houseList(azProjectId, buildNum);

        //得到层号和当前楼层的List<House>对应的map
        Map<String, List<House>> floorHouseMap = houseService.getFloorHouseMap(houseList);
        Map<String,Integer> maxFloorMap = houseService.getMaxFloorMap(azProjectId, buildNum);
        Integer maxFloor = maxFloorMap.get("maxFloor");
        //房源状态
        Map<String, String> colorMap = this.dataDictService.getMapByParentNameForValue_Color("房源状态");
        //房屋类型
        Map<String, String> houseHoldMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");
        Map<String, Object> map = Maps.newHashMap();
        map.put("maxFloor", maxFloor);
        map.put("floorHouseMap", floorHouseMap);
        map.put("colorMap", colorMap);
        map.put("houseHoldMap", houseHoldMap);
        return map;
    }

    /**
     * 房屋结构类型详情
     *
     * @param request
     * @return
     */
    @RequestMapping("houseHoldInfo")
    @ResponseBody
    @RequiresPermissions("houseView:houseHoldInfo")
    public Map houseTypeInfo(HttpServletRequest request) {
        Integer azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        String buildNum = ParamUtils.getString(request, "buildNum", "");
        Map<String, String> houseHoldMap = this.dataDictService.getMapByParentNameForValue_Name("房屋户型");
        Map<String, Map> houseInfoMap = this.houseService.houseHoldInfoMap(azProjectId, buildNum);
        Map houseHoldTotalMap = this.houseService.houseHoldTotalMap(azProjectId, buildNum);
        Map<String, Object> map = Maps.newHashMap();
        map.put("houseHoldMap", houseHoldMap);
        map.put("houseInfoMap", houseInfoMap);
        map.put("houseHoldTotalMap", houseHoldTotalMap);
        return map;
    }

    /**
     * 安置项目数据查看
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("houseView")
    @RequiresPermissions("houseView:houseView")
    public void houseView(HttpServletRequest request, ModelMap modelMap) {
        int houseId = ParamUtils.getInt(request, "houseId", 0);
        House house = this.houseService.findOne(houseId);
        Precondition.checkSqdsArguement(house != null, "数据错误！");
        AzProject azProject = this.azProjectService.findOne(house.getAzProject().getId());
        Precondition.checkSqdsArguement(azProject != null, "数据错误！");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");
        Map<String, DataDict> houseHoldMap = this.dataDictService.getMapByParentNameForValue("房屋户型");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        Map<String, StorageRoom> resultMap = Maps.newHashMap();
        if (house.getContract() != null) {
            Integer contractId = house.getContract().getId();
            List<House> houseList = this.houseService.getHouseListByContractId(contractId);
            modelMap.addAttribute("houseList", houseList);
            List<StorageRoom> storageRoomList = this.storageRoomService.getStorageRoomListByContractId(contractId);
            modelMap.addAttribute("storageRoomList", storageRoomList);
            List<CarLot> carLotList = this.carLotService.getCarLotListByContractId(contractId);
            modelMap.addAttribute("carLotList", carLotList);
            List<String> ids = this.houseBuyPersonService.getBuyHousePersonIdListByHouseId(house.getId());
            List<BuyHousePerson> buyHousePersonList = new ArrayList<>();
            if (ids.size() != 0) {
                buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByBuyHousePersonId(ids, contractId);
            }
            modelMap.addAttribute("buyHousePersonList", buyHousePersonList);
            resultMap = Maps.newHashMap();
            List<Integer> houseIdList = Lists.newArrayList();
            houseIdList.add(house.getId());
            resultMap = this.storageRoomService.getStorageRoomMap(houseIdList);
        }

        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("house", house);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
        modelMap.addAttribute("houseHoldMap", houseHoldMap);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
        modelMap.addAttribute("resultMap", resultMap);
    }

    /**
     * 住宅房源储物间数据查看列表
     */
    @RequestMapping("storageRoomList")
    @RequiresPermissions("houseView:storageRoomList")
    public void storageRoomList(HttpServletRequest request, ModelMap modelMap) {
        Integer projectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(projectId);
        Map<String, Map> storageRoomMap = this.statisticsService.getStorageRoomCountMapByAzProjectId();
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("storageRoomMap", storageRoomMap);
    }

    /**
     * 加载储物间信息数据
     *
     * @return
     */
    @RequestMapping("loadStorageRoomList")
    @ResponseBody
    @RequiresPermissions("houseView:loadStorageRoomList")
    public Map loadStorageRoomList(HttpServletRequest request) {
        Integer projectId = ParamUtils.getInt(request, "azProjectId", 0);
        List<Map> listData = Lists.newArrayList();
        PageBean<StorageRoom> storageRoomPageBean = this.storageRoomService.storageRoomListByProjectId(projectId);
        List<StorageRoom> storageRoomList = storageRoomPageBean.getDatas();
        Map<String, DataDict> houseStatusMap = this.dataDictService.getMapByParentNameForValue("房源状态");
        if (!CollectionUtils.isEmpty(storageRoomList)) {
            Map<String, Object> item = null;
            Integer houseStatus = null;
            for (StorageRoom storageRoom : storageRoomList) {
                item = null;
                houseStatus = null;
                item = Maps.newHashMap();
                item.put("id", storageRoom.getId());
                item.put("buildNum", StringUtils.getNotNull(storageRoom.getBuildNum()));
                item.put("houseNum", StringUtils.getNotNull(storageRoom.getHouseNum()));
                item.put("floor", IntegerUtils.getNotNull(storageRoom.getFloor()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(storageRoom.getBuildArea()));
                item.put("houseStatus", storageRoom.getHouseStatus());
                houseStatus = storageRoom.getHouseStatus().equals(HouseStatus.YULIU.getIndex()) ? HouseStatus.YIXUAN.getIndex() : storageRoom.getHouseStatus();
                item.put("houseStatusStr", StringUtils.dataDictShow(houseStatusMap.get(houseStatus.toString())));
                item.put("chooseHouseDate", DateUtils.dateToStringDefault(storageRoom.getChooseHouseDate()));
                item.put("memo", storageRoom.getMemo());
                listData.add(item);
            }
        }
        return LayUiUtils.page(storageRoomPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 储物间详情查看
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("storageRoomView")
    @RequiresPermissions("houseView:storageRoomView")
    public void storageRoomView(HttpServletRequest request, ModelMap modelMap) {
        int azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        int storageRoomId = ParamUtils.getInt(request, "storageRoomId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        StorageRoom storageRoom = this.storageRoomService.findOne(storageRoomId);
        Precondition.checkSqdsArguement(azProject != null, "数据错误！");
        Precondition.checkSqdsArguement(storageRoom != null, "数据错误！");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        if (storageRoom.getHouse() != null) {
            Integer houseId = storageRoom.getHouse().getId();
            House house = this.houseService.findOne(houseId);
            modelMap.addAttribute("house", house);

            List<String> ids = this.houseBuyPersonService.getBuyHousePersonIdListByHouseId(houseId);
            List<BuyHousePerson> buyHousePersonList = new ArrayList<>();
            if (ids.size() != 0) {
                buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByBuyHousePersonId(ids, storageRoom.getContract().getId());
            }
            modelMap.addAttribute("buyHousePersonList", buyHousePersonList);
        }
        if (storageRoom.getContract() != null) {
            Integer contractId = storageRoom.getContract().getId();
            List<House> houseList = this.houseService.getHouseListByContractId(contractId);
            modelMap.addAttribute("houseList", houseList);
            List<StorageRoom> storageRoomList = this.storageRoomService.getStorageRoomListByContractId(contractId);
            modelMap.addAttribute("storageRoomList", storageRoomList);
            List<CarLot> carLotList = this.carLotService.getCarLotListByContractId(contractId);
            modelMap.addAttribute("carLotList", carLotList);
        }
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("storageRoom", storageRoom);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
    }


    /**
     * 住宅车位数据查看列表
     */
    @RequestMapping("carLotList")
    @RequiresPermissions("houseView:carLotList")
    public void carLotList(HttpServletRequest request, ModelMap modelMap) {
        Integer projectId = ParamUtils.getInt(request, "azProjectId", 0);
        AzProject azProject = this.azProjectService.findOne(projectId);
        Map<String, Map> carLotMap = this.statisticsService.getCarLotCountMapByAzProjectId();
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("carLotMap", carLotMap);
    }

    /**
     * 加载车位信息数据
     *
     * @return
     */
    @RequestMapping("loadCarLotList")
    @ResponseBody
    @RequiresPermissions("houseView:loadCarLotList")
    public Map loadCarLotList(HttpServletRequest request) {
        Integer projectId = ParamUtils.getInt(request, "azProjectId", 0);
        List<Map> listData = Lists.newArrayList();
        PageBean<CarLot> carLotPageBean = this.carLotService.carLotListByProjectId(projectId);
        List<CarLot> carLotList = carLotPageBean.getDatas();
        Map<String, DataDict> houseStatusMap = this.dataDictService.getMapByParentNameForValue("房源状态");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");
        if (!CollectionUtils.isEmpty(carLotList)) {
            Integer houseStatus = null;
            Map<String, Object> item = null;
            for (CarLot carLot : carLotList) {
                houseStatus = null;
                item = null;
                item = Maps.newHashMap();
                item.put("id", carLot.getId());
                item.put("buildNum", StringUtils.getNotNull(carLot.getBuildNum()));
                item.put("houseNum", StringUtils.getNotNull(carLot.getHouseNum()));
                item.put("floor", IntegerUtils.getNotNull(carLot.getFloor()));
                item.put("buildArea", BigDecimalUtils.getNotNullStr(carLot.getBuildArea()));
                item.put("carLotType", StringUtils.dataDictShow(carLotTypeMap.get(carLot.getCarLotType().toString())));
                item.put("houseStatus", carLot.getHouseStatus());
                houseStatus = carLot.getHouseStatus().equals(HouseStatus.YULIU.getIndex()) ? HouseStatus.YIXUAN.getIndex() : carLot.getHouseStatus();
                item.put("houseStatusStr", StringUtils.dataDictShow(houseStatusMap.get(houseStatus.toString())));
                item.put("chooseDate", DateUtils.dateToStringDefault(carLot.getChooseHouseDate()));
                item.put("memo", carLot.getMemo());
                listData.add(item);
            }
        }
        return LayUiUtils.page(carLotPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 车位信息查看
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("carLotView")
    @RequiresPermissions("houseView:carLotView")
    public void carLotView(HttpServletRequest request, ModelMap modelMap) {
        int azProjectId = ParamUtils.getInt(request, "azProjectId", 0);
        int carLotId = ParamUtils.getInt(request, "carLotId", 0);
        AzProject azProject = this.azProjectService.findOne(azProjectId);
        CarLot carLot = this.carLotService.findOne(carLotId);
        Precondition.checkSqdsArguement(azProject != null, "数据错误！");
        Precondition.checkSqdsArguement(carLot != null, "数据错误！");
        Map<String, DataDict> houseTypeMap = this.dataDictService.getMapByParentNameForValue("房屋类型");
        Map<String, DataDict> carLotTypeMap = this.dataDictService.getMapByParentNameForValue("车位类型");

        if (carLot.getContract() != null) {
            Integer contractId = carLot.getContract().getId();
            List<House> houseList = this.houseService.getHouseListByContractId(contractId);
            modelMap.addAttribute("houseList", houseList);
            List<StorageRoom> storageRoomList = this.storageRoomService.getStorageRoomListByContractId(contractId);
            modelMap.addAttribute("storageRoomList", storageRoomList);
            List<CarLot> carLotList = this.carLotService.getCarLotListByContractId(contractId);
            modelMap.addAttribute("carLotList", carLotList);
            List<String> ids = this.carLotBuyPersonService.getBuyHousePersonIdListByCarLotId(carLot.getId());
            List<BuyHousePerson> buyHousePersonList = new ArrayList<>();
            if (ids.size() != 0) {
                buyHousePersonList = this.buyHousePersonService.getBuyHousePersonListByBuyHousePersonId(ids, contractId);
            }
            modelMap.addAttribute("buyHousePersonList", buyHousePersonList);
        }
        modelMap.addAttribute("azProject", azProject);
        modelMap.addAttribute("carLot", carLot);
        modelMap.addAttribute("houseTypeMap", houseTypeMap);
        modelMap.addAttribute("carLotTypeMap", carLotTypeMap);
    }

}
